package com.ysy.logistics.controller.echarts;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.echarts.*;
import com.ysy.logistics.entities.stock.StoreHouse;
import com.ysy.logistics.service.device.DeviceService;
import com.ysy.logistics.service.sale.CusOrderService;
import com.ysy.logistics.service.sale.CustomerService;
import com.ysy.logistics.service.stock.StoreGoodsService;
import com.ysy.logistics.service.stock.StoreHouseService;
import com.ysy.logistics.service.supplier.SupOrderService;
import com.ysy.logistics.service.supplier.SupplierService;
import com.ysy.logistics.service.system.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/4/15 9:08
 **/
@Slf4j
@RestController
@RequestMapping(value = "/echarts", name = "统计图")
@Tag(name = "统计图", description = "统计图信息")
@CrossOrigin
public class EchartsController {

    @Resource
    private SupOrderService supOrderService;
    @Resource
    private CusOrderService cusOrderService;
    @Resource
    private StoreHouseService storeHouseService;
    @Resource
    private StoreGoodsService storeGoodsService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private DeviceService deviceService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/order/{days}")
    @Operation(summary = "订单统计", description = "订单统计")
    public ResultData<Echarts> orderTotal(@PathVariable("days") Integer days) {
        //参数判断
        if ( days == null || days < 1) {
            return ResultData.fail("参数异常");
        }
        Echarts echarts = new Echarts();
        List<String> dateArray = new ArrayList<>();
        //构建日期
        LocalDate today = LocalDate.now();
        for (int i = 0; i < days; i++) {
            LocalDate date = today.minusDays(i);
            dateArray.add(date.toString());
        }
        //数组翻转
        Collections.reverse(dateArray);
        List<Series> seriesList = new ArrayList<>();
        //采购单统计
        seriesList.add(supOrderService.getTotal(dateArray));
        //销售单统计
        seriesList.add(cusOrderService.getTotal(dateArray));
        //应为有预测数据，所以添加明天日期
        dateArray.add(today.minusDays(-1).toString());

        echarts.setDate(dateArray)
                .setSeriesList(seriesList);

        return ResultData.success(echarts);
    }

    @GetMapping("/storeHouseUsed")
    @Operation(summary = "仓库使用率",description = "仓库使用率统计")
    public ResultData<List<HouseUsage>> storeHouseUsed() {
        return ResultData.success(storeHouseService.totalUsage());
    }

    @GetMapping("/goodsTypeTotal")
    @Operation(summary = "商品类型统计",description = "商品类型统计")
    public ResultData<List<PieTotal>> goodsTypeTotal(){
        return ResultData.success(storeGoodsService.goodsTypetotal());
    }
    @GetMapping("/userRoleTotal")
    @Operation(summary = "用户角色统计",description = "用户角色统计")
    public ResultData<List<PieTotal>> userRoleTotal(){
        return ResultData.success(userRoleService.userRoleTotal());
    }

    @GetMapping("/test")
    public ResultData<List<Economize>> test(){
        List<Economize> economizeList =new ArrayList<>();
        Economize economize = new Economize();
        economize.setSource("四川");
        economize.setCount(2);
        economizeList.add(economize);
        return ResultData.success(economizeList);
    }

    @GetMapping("/total")
    @Operation(summary = "统计",description = "统计")
    public ResultData<Map<String,Long>> total(){
        Map<String,Long> result = new HashMap<>();
        //仓库总数统计
        result.put("仓库总数",storeHouseService.count());
        //供应商总数统计
        result.put("供应商总数",supplierService.count());
        //客户总数统计
        result.put("客户总数",customerService.count());
        //设备总数统计
        result.put("设备总数 ", deviceService.count());
        return ResultData.success(result);
    }
}
